package cn.iocoder.yudao.module.def.controller.admin.mysql;


import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.def.controller.admin.mysql.service.SystemMysqlBackupsService;
import cn.iocoder.yudao.module.def.controller.admin.mysql.service.SystemMysqlBackupsServiceImpl;
import cn.iocoder.yudao.module.def.controller.admin.mysql.vo.BackupsPageReqVO;
import cn.iocoder.yudao.module.def.controller.admin.mysql.vo.BackupsRespVO;
import cn.iocoder.yudao.module.def.controller.admin.mysql.vo.BackupsDO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * ClassName:SystemMysqlBackupsController
 * 类描述： MySQL数据备份接口
 */
@RestController
@Tag(name = "MySQL数据备份")
@RequestMapping(value = "/mysql/baskups")
public class SystemMysqlBackupsController {

    /**
     * 数据库用户名
     */
    @Value("${spring.datasource.dynamic.datasource.master.username}")
    private String userName;

    /**
     * 数据库密码
     */
    @Value("${spring.datasource.dynamic.datasource.master.password}")
    private String password;

    /**
     * 数据库url
     * spring.datasource.dynamic.datasource.master.url
     */
    @Value("${spring.datasource.dynamic.datasource.master.url}}")
    private String url;

    /**
     * Windows数据库备份地址
     */
    @Value("${spring.datasource.win-path}")
    private String windowsPath;

    /**
     * Linux数据库备份地址
     */
    @Value("${spring.datasource.linux-path}")
    private String linuxPath;

    @Autowired
    private SystemMysqlBackupsService systemMysqlBackupsService;

    @Operation(summary = "获取所有备份数据列表")
    @GetMapping("/backupsList")
    public CommonResult backupsList() {
        List<BackupsDO> backupDOS = systemMysqlBackupsService.selectBackupsList();
        return success(backupDOS);
    }

    @Operation(summary = "MySQL备份")
    @PostMapping("/mysqlBackups")
    public CommonResult mysqlBackups(@RequestBody Map<String, Object> map) {
        String path = null;
        // 获取操作系统名称
        String osName = System.getProperty("os.name").toLowerCase();
        Object bfdz = map.get("bfdz");
        if (bfdz != null) {
            path = bfdz.toString();
        }else{
            if (SystemMysqlBackupsServiceImpl.isSystem(osName)) {
                // Windows
                path = this.windowsPath;
            } else {
                // Linux
                path = this.linuxPath;
            }
        }

        // 数据库用户名
        String userName = this.userName;
        // 数据库密码
        String password = this.password;
        // 数据库地址
        String url = this.url;
        // 调用备份
        Object systemMysqlBackups = systemMysqlBackupsService.mysqlBackups(path, url, userName, password);
        return success(systemMysqlBackups).setMsg("操作成功");
    }

    @Operation(summary = "恢复数据库")
    @PutMapping("/rollback")
    public CommonResult rollback( @RequestBody Map<String, Object> map) {
        Long id = Long.valueOf(map.get("id").toString());
        // 数据库用户名
        String userName = this.userName;
        // 数据库密码
        String password = this.password;
        // 根据id查询查询已有的信息
        BackupsDO smb = systemMysqlBackupsService.selectListId(id);
        // 恢复数据库
        Object rollback = systemMysqlBackupsService.rollback(smb, userName, password);
        // 更新操作次数
        systemMysqlBackupsService.updateById(smb);
        return  success(rollback).setMsg("操作成功");
    }

    @GetMapping("/page")
    @Operation(summary = "获得MySQL数据备份分页")
    @PreAuthorize("@ss.hasPermission('mysql:backups:query')")
    public CommonResult<PageResult<BackupsRespVO>> getBackupsPage(@Valid BackupsPageReqVO pageReqVO) {
        PageResult<BackupsDO> pageResult = systemMysqlBackupsService.getBackupsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BackupsRespVO.class));
    }
}
